﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class SysUserGroups : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    private void getUserGroupsList(string strGroupName)
    {
        GroupsDB objDB = new GroupsDB();
        gvUserGroupList.DataSource = objDB.getUserGroupList(strGroupName);
        gvUserGroupList.DataBind();
        objDB.CloseDB();
    }

    private void getUserGroupsList()
    {
        getUserGroupsList(txtSearchGroupName.Text.Trim());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.Settings + "|" + ISitemap.UserSettings.ToString() + "| " + ISitemap.UserGroupManagement.ToString();
            getUserGroupsList("");
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSearch)
        {
            getUserGroupsList();
        }
        else if (sender == btnNew)
        {
            Response.Redirect("SysUserGroupsAdd.aspx");
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblGroupType = (Label)e.Row.FindControl("lblGroupType");
            ImageButton btnEdit = (ImageButton)e.Row.FindControl("btnEdit");
            ImageButton btnTrash = (ImageButton)e.Row.FindControl("btnTrash");

            btnTrash.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");

            btnEdit.Visible = lblGroupType.Text == "Normal";
            btnTrash.Visible = btnEdit.Visible;
        }
    }

    protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
        string strRowno = gvUserGroupList.DataKeys[curRow.RowIndex].Values["Rowno"].ToString();
        string strUserGroup = gvUserGroupList.DataKeys[curRow.RowIndex].Values["GroupName"].ToString();

        if (e.CommandName == "Edit")
        {
            Response.Redirect("SysUserGroupsUpdate.aspx?groupid=" + strRowno);
        }
        else if (e.CommandName == "Trash")
        {
            GroupsDB objDB = new GroupsDB();

            if (objDB.Trash(strRowno) == 0)
            {
                new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_usergroup_manage, "Delete User Group Success! User Group: " + strUserGroup);
                objDB.CloseDB();
                Jscript.Alert("Delete User Group Success!", Page);
                getUserGroupsList();
            }
            else
            {
                new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_usergroup_manage, "Delete User Group Failure! User Group: " + strUserGroup);
                objDB.CloseDB();
                Jscript.Alert("Delete User Group Failure!", Page);
            }
        }
   }
}
